#include "kurcaci.h"
#include "kurcacislash.h"
#include "plant.h"
#include "field.h"
#include <iostream>
#include <cmath>
using namespace std;

KurcaciSlash::KurcaciSlash() {
}

KurcaciSlash::KurcaciSlash(const KurcaciSlash& slashy) {
}

KurcaciSlash::~KurcaciSlash() {
}

void KurcaciSlash::slash(field* afield) {
	int x = vektor.x;
	int y = vektor.y;
	if (afield->IsSlashable(x,y))
	{
		afield->DeleteObject(x,y);
		afield->InsertObject(new Tanah(false,false),x,y);
	}
}

void KurcaciSlash::walk(field * afield, int xplayer, int yplayer,int xkurcwater,int ykurcwater,int xkurcharvest,int ykurcharvest,int xkurcslash,int ykurcslash) {
	int nearx = NearestPoint(afield).x;
	int neary = NearestPoint(afield).y;
	int selisihx = nearx - GetX();
	int selisihy = neary - GetY();
	if(status_sleep==false && (nearx!=-1 && neary!=-1)) {//jika tidak sedang tidur
		if(searchdir==-1) {
			if(abs(selisihx)<abs(selisihy)){
				if(selisihx>0) {
					moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
				}else if(selisihx<0) {
					moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
				}else{
					if(selisihy>1){
						moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
					}else if(selisihy<-1){
						moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
					}else if(selisihy==1){
						turn_down();
						slash(afield);
					}else{
						turn_up();
						slash(afield);
					}
				}
			}else{
				if(selisihy>0) {
					moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
				}else if(selisihy<0){
					moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
				}else{//jika selisihy==0 ato target udah sejajar
					if(selisihx>1) {
						moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
					}else if(selisihx<-1){
						moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
					}else if(selisihx==1){
						turn_right();
						slash(afield);
					}else {
						turn_left();
						slash(afield);
					}
				}
			}
		}else{
			if(face!=searchdir) {
				setFace(searchdir);
			}else{
				if(faceto==0){
					if(((location.left.x!=xplayer || location.left.y!=yplayer) && (location.left.x!=xkurcharvest || location.left.y != ykurcharvest) && (location.left.x!=xkurcwater || location.left.y!=ykurcwater)) && afield->IsWalkable(location.left.x,location.left.y)){
						setFace(faceto);
						moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						searchdir = -1;
					}else{
						if(searchdir==0) {
							moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==1) {
							moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==2) {
							moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==3) {
							moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}
					}
				}else if(faceto==1) {
					if(((location.right.x!=xplayer || location.right.y!=yplayer) && (location.right.x!=xkurcharvest || location.right.y != ykurcharvest) && (location.right.x!=xkurcwater || location.right.y!=ykurcwater)) && afield->IsWalkable(location.right.x,location.right.y)){
						setFace(faceto);
						moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						searchdir = -1;
					}else{
						if(searchdir==0) {
							moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==1) {
							moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==2) {
							moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==3) {
							moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}
					}
				}else if(faceto==2) {
					if(((location.up.x!=xplayer || location.up.y!=yplayer) && (location.up.x!=xkurcharvest || location.up.y != ykurcharvest) && (location.up.x!=xkurcwater || location.up.y!=ykurcwater)) && afield->IsWalkable(location.up.x,location.up.y)){
						setFace(faceto);
						moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						searchdir = -1;
					}else{
						if(searchdir==0) {
							moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==1) {
							moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==2) {
							moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==3) {
							moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}
					}
				}else if(faceto==3) {
					if(((location.down.x!=xplayer || location.down.y!=yplayer) && (location.down.x!=xkurcharvest || location.down.y != ykurcharvest) && (location.down.x!=xkurcwater || location.down.y!=ykurcwater)) && afield->IsWalkable(location.down.x,location.down.y)){
						setFace(faceto);
						moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						searchdir = -1;
					}else{
						if(searchdir==0) {
							moveLeft(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==1) {
							moveRight(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==2) {
							moveUp(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}else if(searchdir==3) {
							moveDown(afield,xplayer, yplayer,xkurcwater,ykurcwater,xkurcharvest,ykurcharvest,xkurcslash,ykurcslash);
						}
					}
				}
			}
		}
	}
}
	
//seharusnya mengembalikan nilai point
point KurcaciSlash::NearestPoint(field* afield) {
	int nowX = GetX();
	int nowY = GetY();
	int i, j;
	int imax, jmax;
	point pos, result;
	float hasil = 0.0;
	float max = 1000;
	found = false;
	
	for (int i=0;i<13;i++)
	{
		for (int j=0;j<13;j++)
		{
			if (afield->IsSlashable(j,i) && dynamic_cast<Plant*>(afield->getObject(j,i))->isMati()) {
				found =  true;
				hasil = sqrt(((nowX - j) * (nowX - j)) + ((nowY - i) * (nowY - i)));
				if (hasil < max) {
					max = hasil;
					imax = i;
					jmax = j;
				}
			}
		}
	}
	result.x = jmax;
	result.y = imax;
	if(found) {
		return result;
	}else{
		result.x = -1;
		result.y = -1;
		return result;
	}
}


	

